package cn.dw.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;

import cn.dw.dao.PeopleDao;
import cn.dw.domain.People;
import cn.dw.service.PeopleService;
import cn.dw.utils.BeanTool;

public class PeopleServiceImpl implements PeopleService {
	private PeopleDao pd;

	public People login(People p) {
		People people = this.pd.getByUserName(p.getUsername());
		if (people == null) {
			throw new RuntimeException("用户名不存在!");
		}
		if (!p.getPassword().equals(people.getPassword())) {
			throw new RuntimeException("密码不正确!");
		}
		return people;
	}

	public People getById(Integer id) {
		return (People) this.pd.getById(id);
	}

	public void update(People peo) {
		if (peo.getNumb() == null) {
			this.pd.save(peo);
		} else {
			People p = (People) this.pd.getById(peo.getNumb());
			BeanUtils.copyProperties(peo, p, BeanTool.getNullProperties(peo));
		}
	}

	public List<Map<String, Object>> check(Integer id) {
		List<People> peoples = this.pd.getByPointId(id);

		People people = new People();

		List<Map<String, Object>> openidList = new ArrayList<>();

		Map<String, Object> map = null;
		if (peoples.size() > 0) {
			for (int i = 0; i < peoples.size(); i++) {
				people = (People) peoples.get(i);
				if (people.getIfin().intValue() == 1) {
					map = new HashMap<>();

					map.put("peopleid", people.getNumb());

					map.put("openid", people.getOpenid());

					map.put("peoplename", people.getName());

					openidList.add(map);
				}
			}
		}
		return openidList;
	}

	public List<People> getAll() {
		return this.pd.getAll();
	}

	public void setPd(PeopleDao pd) {
		this.pd = pd;
	}

	public boolean getByOpenId(String openid) {
		return this.pd.getByOpenId(openid);
	}

	public void save(People peo) {
		this.pd.save(peo);
	}

	public void Insert(String sql) {
		this.pd.Insert(sql);
	}

	public List<People> getByPointId(Integer id) {
		return this.pd.getByPointId(id);
	}
}
